﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
public partial class UserManagement_AddUser : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TeacherModule ta = new TeacherModule();
            List<V_School> vs = new List<V_School>();
            vs = ta.GetSchool();
            dlschool.DataSource = vs;
            dlschool.DataBind();

            if (this.CurrentUser.SchoolID == 0)
            {
            }
            else
            {
                dlschool.SelectedValue = this.SchoolID.ToString();
                dlschool.Enabled = false;
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (LoginName() == true)
        {

            V_user user = new V_user();
            UserModule um = new UserModule();
            user.SchoolID = Convert.ToInt32(dlschool.SelectedValue.ToString());
            user.create_time = DateTime.Now;
            user.update_time = DateTime.Now;
            user.VU_email = txtVU_email.Text.Trim();
            user.VU_loginname = txtVU_loginname.Text.Trim();
            user.VU_mobile = txtVU_mobile.Text.Trim();
            user.VU_Password = StringUtils.EncryptPassWord( txtVU_Password.Text.Trim(),StringUtils.PassFormat.MD5);
            user.VU_realname = txtVU_realname.Text.Trim();
           // user.IsAvailable = true;//默认启用
            user.IsAvailable = this.chkIsEnable.Checked ? true : false;
            user.IsBuyingAccount = Convert.ToInt32(this.radIsBuyAccount.SelectedValue);
            try
            {
                int i = um.AddUser(user);
                if (i > 0)
                {
                    V_User_Log userLog = new V_User_Log();
                    userLog.UserId = i;
                    userLog.SchoolId = user.SchoolID;
                    userLog.OperateDateTime = DateTime.Now;
                    userLog.IsEnable = user.IsAvailable ? 1 : 0;
                    new UserModule().StartOrStopUserLog(userLog);
                    PageHelper.AlterMsgAndRedirect("添加学生成功！", "ViewUserInfoList.aspx");
                }
                else
                {
                    // PageHelper.AlterMsg("添加学生失败");
                    lblMess.Text = "添加学生失败";
                }
            }
            catch (Exception ex)
            {
                lblMess.Text = "添加失败，登陆名已存在，请输入新值，稍后再试！";

            }
        }
       
    }
    //检查登录名是否相同
    private bool LoginName()
    {
        V_user user = new V_user();
        UserModule uname = new UserModule();
        string name = txtVU_loginname.Text;
        if (uname.IfNameExist(int.Parse(this.dlschool.SelectedValue),name))
        {

            lblName.Text = "此名可用";
            return true;
        }
        else
        {

            lblName.Text = "该名已被占用";
            return false;
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtVU_email.Text = null;
        txtVU_loginname.Text = null;
        txtVU_mobile.Text = null;
        txtVU_Password.Text = null;
        txtVU_realname.Text = null;
    }
}
